package com.greet.controller.user;

import com.greet.context.BaseContext;
import com.greet.entity.AddressBook;
import com.greet.result.Result;
import com.greet.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public Result<List<AddressBook>> getByUserId(){
        List<AddressBook> addressBooks = addressBookService.getByUserId(BaseContext.getCurrentId());
        return Result.success(addressBooks);
    }

    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @GetMapping("/default")
    public Result<AddressBook> getByIsDefault(){
        AddressBook addressBook = addressBookService.getByIsDefault(BaseContext.getCurrentId(), 1);
        return Result.success(addressBook);
    }

    @PutMapping
    public Result setById(@RequestBody AddressBook addressBook){
        addressBookService.setById(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(BaseContext.getCurrentId(), Long.valueOf(addressBook.getId()));
        return Result.success();
    }

    @PostMapping
    public Result addAddress(@RequestBody AddressBook addressBook){
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteById(Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }
}
